package lib.util;

import lib.Dao.Dbutil;
import lib.Dao.TokenDao;
import lib.servlet.AuthUtil;
import net.sf.json.JSONObject;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;


public class PostMessage {
    public String posttoopenid(String openid,String changetype,String coursedata) throws SQLException {

        TokenDao tokenDao = new TokenDao();
        // 获取token
        String token = null;
        try {
            token = tokenDao.gettoken();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String currettime=df.format(new Date());// new Date()为获取当前系统时间


        String postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser",openid);   // openid
        jsonObject.put("template_id", "FjLNSiQn2_P6sVf9MlDO1hwPBrR58HS0GCBL0PSlBDI");
        jsonObject.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx291b25f289a25814&redirect_uri=http://www.eyehelper.top/CallBackServlet&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");

        JSONObject data = new JSONObject();
        JSONObject first = new JSONObject();
        first.put("value", "亲爱的用户，您预约的课程余量发生变化");
        first.put("color", "#173177");
        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", "课程余量提醒");
        keyword1.put("color", "#173177");
        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", changetype);
        keyword2.put("color", "#173177");
        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", currettime);
        keyword3.put("color", "#173177");
        JSONObject remark = new JSONObject();
        remark.put("value", coursedata);
        remark.put("color", "#173177");

        data.put("first", first);
        data.put("keyword1", keyword1);
        data.put("keyword2", keyword2);
        data.put("keyword3", keyword3);
        data.put("remark", remark);

        jsonObject.put("data", data);
        String json = jsonObject.toString();
        System.out.println(json);
        String result = AuthUtil.HttpPostWithJson(postUrl, json);
        System.out.println(result);
        return result;
    }
}

